package com.iris.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @date : 2017年9月8日 上午11:50:05
 * @author : Iris.Xiao
 * @version : 1.0
 * @description : 
*/
public class HttpClientUtil {

	public static String doPost(String url,Map<String,String[]> params){
		HttpClient httpClient =null;
		String result =null;
		try{
			httpClient  = new DefaultHttpClient();
			HttpPost  httpPost = new HttpPost(url);
			httpPost.setHeader("channel", "9a9f57c628fc455386f97f734fc3c060");
			httpPost.setHeader("packagename", "com.zillion.besthdmovies");
			httpPost.setHeader("appVersion", "70");
			if(null != params){
				List<NameValuePair> nvps=new ArrayList<NameValuePair>();
				for(Entry<String, String[]> set :params.entrySet()){
					String key = set.getKey();
					String values [] = set.getValue();
					for(String value : values){
						nvps.add(new BasicNameValuePair(key, value));
					}
				}
				httpPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
			}
			HttpResponse httpResponse =httpClient.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			if(null != entity){
				result = EntityUtils.toString(entity, "utf-8");
			}
			EntityUtils.consume(entity);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(null !=httpClient){
				httpClient.getConnectionManager().shutdown();
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		HttpClientUtil.doPost("http://gbox.api.3dzed.com/gbox/client/config.action", null);
	}
}
